﻿using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;

public partial class MySql综合 : Form
{
    // public static string[] args;

    public MySql综合()
    {
        InitializeComponent();
    }

    public MySql综合(string[] args)
    {
        InitializeComponent();

        string s1 = "";
        foreach (string s in args)
        {
            s1 = s1 + s + " ";
        }

        string sql = s1;
        connect_database();
        this.tabControlMain.SelectedTab = this.tabPage2;
        this.richTextBox1.Text = sql;

        ExcuteSql(sql);
    }

    //static MySql综合 myObj = new MySql综合();

    //static public MySql综合 GetObj()
    //{
    //    return myObj;

    //}

    /// <summary>
    /// 连接数据库
    /// </summary>
    private void connect_database()
    {
        try
        {
            treeView1.Nodes.Clear();
            TreeNode tn1 = new TreeNode();
            tn1.Text = this.textBoxServer.Text.ToString();
            tn1.Tag = "root";

            treeView1.Nodes.Add(tn1);

            if (this.textBoxServer.Text == "") { MessageBox.Show("非空"); return; }
            //if (this.textBoxDataBase.Text == "") { MessageBox.Show("非空"); return; }

            string datasource = this.textBoxServer.Text;
            //string database = this.textBoxDataBase.Text;
            string uid = this.textBoxUid.Text;
            string pwd = this.textBoxPass.Text;

            string strCon = "server=" + datasource + ";user id=" + uid + "; pooling=false;charset=gb2312; password=" + pwd + "; Allow Zero Datetime=True";

            MysqlHelper.strConnString = strCon;

            string strSql = "SHOW DATABASES";

            MySql.Data.MySqlClient.MySqlDataReader thisReader = MysqlHelper.GetReader(strSql);

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("数据库");

            while (thisReader.Read())
            {
                DataRow dr1 = dt1.NewRow();
                dr1[0] = thisReader[0].ToString();
                dt1.Rows.Add(dr1);

                TreeNode tn = new TreeNode();
                tn.Text = thisReader[0].ToString();
                tn.Tag = "database";

                this.treeView1.Nodes[0].Nodes.Add(tn);
            }

            this.dataGridViewTable.DataSource = dt1;

            thisReader.Close();
        }
        catch (Exception ee)
        {
            MessageBox.Show("连接失败....");
        }
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void buttonTestConect_Click(object sender, EventArgs e)
    {
        connect_database();
    }

    private void BindTree()
    {
    }

    private void MySql综合_Load(object sender, EventArgs e)
    {
        string strPath = Application.StartupPath + "\\Config" + "\\XMLFileMysql.xml";
        XmlDocument doc = new XmlDocument();
        doc.Load(strPath);

        XmlNodeList nodeList = doc.SelectSingleNode("root").ChildNodes;
        //string strNode = "";

        foreach (XmlNode xn in nodeList)
        {
            if (xn.Name == "mysql")
            {
                string server = "";
                string name = "";
                string user = "";
                string password = "";

                foreach (XmlNode xn1 in xn.ChildNodes)
                {
                    if (xn1.Name == "server")
                    {
                        server = xn1.InnerText;
                    }
                    else if (xn1.Name == "name")
                    {
                        name = xn1.InnerText;
                    }
                    else if (xn1.Name == "user")
                    {
                        user = xn1.InnerText;
                    }
                    else if (xn1.Name == "password")
                    {
                        password = xn1.InnerText;
                    }
                }

                string result = "";
                result += "name:" + name + ";";
                result += "server:" + server + ";";
                result += "user:" + user + ";";
                result += "password:" + password + ";";

                this.listBox1.Items.Add(result);
            }
        }
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            string result = listBox1.SelectedItem.ToString();
            string[] result1 = result.Split(';');

            this.textBoxServer.Text = result1[1].Replace("server:", "").Trim();
            this.textBoxUid.Text = result1[2].Replace("user:", "").Trim();
            this.textBoxPass.Text = result1[3].Replace("password:", "").Trim();
        }
        catch (Exception ee)
        {
        }
    }

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        try
        {
            if (this.treeView1.SelectedNode.Tag.ToString() == "root")
            {
                treeView1.SelectedNode.Expand();
                return;
            }

            treeView1.SelectedNode.Nodes.Clear();

            if (this.treeView1.SelectedNode.Tag.ToString() == "database")
            {
                string strSql = "SHOW  TABLES from " + treeView1.SelectedNode.Text + "";

                MySqlDataReader thisReader = MysqlHelper.GetReader(strSql);

                while (thisReader.Read())
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = thisReader[0].ToString();
                    tn.Tag = "table";

                    treeView1.SelectedNode.Nodes.Add(tn);
                    treeView1.SelectedNode.Expand();
                }

                thisReader.Close();
            }
            else if (this.treeView1.SelectedNode.Tag.ToString() == "table")
            {
                //*********************************************************
                //
                //*********************************************************
                string strDataBase = this.treeView1.SelectedNode.Parent.Text;
                string strTable = this.treeView1.SelectedNode.Text;
                string strSql = "select * from " + strDataBase + "." + strTable + " limit 0,100";

                this.dataGridView1.DataSource = MysqlHelper.GetDataTable(strSql);

                //*********************************************************
                //SHOW full COLUMNS FROM assets.device
                //*********************************************************
                //strSql = "SHOW COLUMNS FROM " + strDataBase + "." + strTable + "";
                strSql = "SHOW full COLUMNS FROM " + strDataBase + "." + strTable + "";
                DataTable dt = MysqlHelper.GetDataTable(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    string field = dr["field"].ToString();
                    string type = dr["type"].ToString();
                    string nullvalue = dr["null"].ToString();
                    string key = dr["key"].ToString();
                    string extra = dr["extra"].ToString();
                    string comment = dr["comment"].ToString();

                    string result = "";
                    result += field;
                    result += " [";
                    result += type + ",";

                    if (nullvalue.ToLower() == "yes")
                    {
                        result += "null" + ",";
                    }
                    else
                    {
                        result += "not null" + ",";
                    }

                    result += key + ",";

                    result += extra + ",";

                    result += comment + "]";

                    TreeNode tn = new TreeNode();
                    tn.Text = result;
                    tn.Tag = "column";
                    treeView1.SelectedNode.Nodes.Add(tn);
                }
            }
        }
        catch (Exception ee)
        {
        }
    }

    private void ExcuteSql(string strSql)
    {
        try
        {
            MySqlDataReader thisReader = MysqlHelper.GetReader(strSql);

            DataTable dt = new DataTable();

            for (int i = 0; i < thisReader.FieldCount; i++)
            {
                dt.Columns.Add(thisReader.GetName(i));
            }

            while (thisReader.Read())
            {
                DataRow dr1 = dt.NewRow();
                for (int i = 0; i < thisReader.FieldCount; i++)
                {
                    dr1[i] = thisReader[i].ToString();
                }

                dt.Rows.Add(dr1);
            }

            this.dataGridView1.DataSource = dt;

            dt = MysqlHelper.DealTable_Str(dt);

            thisReader.Close();

            this.tabControl1.SelectedTab = tabPageResult;
            richTextBoxMessage.Text = "";
        }
        catch (Exception ee)
        {
            this.tabControl1.SelectedTab = tabPageMessage;
            richTextBoxMessage.Text = ee.ToString();

            this.dataGridView1.DataSource = null;
        }
    }

    /// <summary>
    ///执行
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void toolStripLabel1_Click(object sender, EventArgs e)
    {
        if (this.richTextBox1.SelectedText == "")
        {
            string strSql = richTextBox1.Text;

            ExcuteSql(strSql);
        }
        else
        {
            string strSql = richTextBox1.SelectedText;

            ExcuteSql(strSql);
        }
    }

    private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        //if(e.KeyChar)
    }

    private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode.ToString() == "F5")
        {
            if (this.richTextBox1.SelectedText == "")
            {
                string strSql = richTextBox1.Text;

                ExcuteSql(strSql);
            }
            else
            {
                string strSql = richTextBox1.SelectedText;

                ExcuteSql(strSql);
            }
        }
    }

    /// <summary>
    /// 导出结构
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void toolStripLabel2_Click(object sender, EventArgs e)
    {
        if (this.treeView1.SelectedNode.Tag.ToString() == "table")
        {
            StringBuilder strTrans = new StringBuilder();

            string database = treeView1.SelectedNode.Parent.Text;
            string table = treeView1.SelectedNode.Text;

            strTrans.Append("SET NAMES utf8;\n");
            strTrans.Append("\n");

            strTrans.Append("create database if not exists`" + database + "`;\n");
            strTrans.Append("\n");
            strTrans.Append("USE`" + database + "`;\n");
            strTrans.Append("\n");

            strTrans.Append("/*Table structure for table`" + table + "` */\n");
            strTrans.Append("\n");
            strTrans.Append("DROP TABLE IF EXISTS`" + table + "`;\n");

            strTrans.Append("CREATE TABLE`" + table + "` (\n");

            string result = "";
            string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
            DataTable dt = MysqlHelper.GetDataTable(strSql);
            foreach (DataRow dr in dt.Rows)
            {
                string field = dr["field"].ToString();
                string type = dr["type"].ToString();
                string nullvalue = dr["null"].ToString();
                string key = dr["key"].ToString();
                string extra = dr["extra"].ToString();
                string comment = dr["comment"].ToString();

                result += "`" + field + "`";
                result += " ";
                result += type + " ";

                if (nullvalue.ToLower() == "yes")
                {
                    result += "null" + " ";
                }
                else
                {
                    result += "not null" + " ";
                }

                if (key.ToLower() == "pri")
                {
                    result += " PRIMARY KEY " + " ";
                }
                else
                {
                }

                result += "COMMENT '" + extra + "', \n";
            }

            result = result.Substring(0, result.LastIndexOf(","));
            strTrans.Append(result);

            strTrans.Append(" \n");
            strTrans.Append(")  DEFAULT CHARSET=latin1;\n");

            string strDes = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            strDes = strDes.Replace(@"\\", @"\");
            strDes = Path.Combine(strDes, "1.sql");

            Encoding encode = Encoding.UTF8;
            StreamWriter sw = new StreamWriter(strDes, false, encode);
            sw.Write(strTrans.ToString());
            sw.Flush();
            sw.Close();

            Process.Start(strDes);
        }
    }

    /// <summary>
    /// 导出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        if (this.treeView1.SelectedNode.Tag.ToString() == "table")
        {
            StringBuilder strTrans = new StringBuilder();

            string database = treeView1.SelectedNode.Parent.Text;
            string table = treeView1.SelectedNode.Text;

            strTrans.Append("SET NAMES utf8;\n");
            strTrans.Append("\n");

            strTrans.Append("create database if not exists`" + database + "`;\n");
            strTrans.Append("\n");
            strTrans.Append("USE`" + database + "`;\n");
            strTrans.Append("\n");

            strTrans.Append("/*Table structure for table`" + table + "` */\n");
            strTrans.Append("\n");
            strTrans.Append("DROP TABLE IF EXISTS`" + table + "`;\n");

            strTrans.Append("CREATE TABLE`" + table + "` (\n");

            string result = "";
            string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
            DataTable dt = MysqlHelper.GetDataTable(strSql);
            foreach (DataRow dr in dt.Rows)
            {
                string field = dr["field"].ToString();
                string type = dr["type"].ToString();
                string nullvalue = dr["null"].ToString();
                string key = dr["key"].ToString();
                string extra = dr["extra"].ToString();
                string comment = dr["comment"].ToString();

                result += "`" + field + "`";
                result += " ";
                result += type + " ";

                if (nullvalue.ToLower() == "yes")
                {
                    result += "null" + " ";
                }
                else
                {
                    result += "not null" + " ";
                }

                if (key.ToLower() == "pri")
                {
                    result += " PRIMARY KEY " + " ";
                }
                else
                {
                }

                result += "COMMENT '" + extra + "', \n";
            }

            result = result.Substring(0, result.LastIndexOf(","));
            strTrans.Append(result);

            strTrans.Append(" \n");
            strTrans.Append(")  DEFAULT CHARSET=latin1;\n");

            strTrans.Append("\n");
            strTrans.Append("/*Data for the table `student` */\n");

            strSql = "select * FROM " + database + "." + table + "";
            dt = MysqlHelper.GetDataTable(strSql);

            string strFields = "";
            string strValues = "";

            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.AutoIncrement != true)
                {
                    strFields += "`" + dc.ColumnName + "`,";
                }
            }

            foreach (DataRow dr in dt.Rows)
            {
                string strTemp = "";
                foreach (DataColumn dc in dt.Columns)
                {
                    if (dc.AutoIncrement != true)
                    {
                        strTemp += "'" + dr[dc.ColumnName].ToString() + "',";
                    }
                }

                strValues += "(" + strTemp.Substring(0, strTemp.Length - 1) + ")" + ",";
            }

            strFields = strFields.Substring(0, strFields.Length - 1);
            strValues = strValues.Substring(0, strValues.Length - 1) + ";";
            strTrans.Append("insert into `" + database + "`.`" + table + "` (" + strFields + ") values " + strValues + "\n");

            strTrans.Append("\n");

            string strDes = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            strDes = strDes.Replace(@"\\", @"\");
            strDes = Path.Combine(strDes, "1.sql");

            Encoding encode = Encoding.UTF8;
            StreamWriter sw = new StreamWriter(strDes, false, encode);
            sw.Write(strTrans.ToString());
            sw.Flush();
            sw.Close();

            Process.Start(strDes);
        }
    }

    /// <summary>
    /// 导入
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void toolStripLabel3_Click(object sender, EventArgs e)
    {
        string strDes = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        strDes = strDes.Replace(@"\\", @"\");
        strDes = Path.Combine(strDes, "1.sql");

        Encoding encode = Encoding.UTF8;
        StreamReader sw = new StreamReader(strDes);

        string strResult = sw.ReadToEnd();
        sw.Close();

        ExcuteSql(strResult);

        MessageBox.Show("ok");
    }

    /// <summary>
    ///add
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void toolStripButton2_Click(object sender, EventArgs e)
    {
        //insert into grade(学号，姓名，数学) values(123,"周润发"，70)

        StringBuilder strTrans = new StringBuilder();

        this.richTextBox1.Text = "";
        if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
        {
            strTrans.Append("insert into grade(学号，姓名，数学) values(123,\"周润发\"，70)\n");
            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
        else
        {
            string database = this.treeView1.SelectedNode.Parent.Text;
            string table = this.treeView1.SelectedNode.Text;

            //string strSql = "select * FROM " + database + "." + table + "";
            //DataTable dt = MysqlHelper.GetDataTable(strSql);

            string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
            DataTable dt = MysqlHelper.GetDataTable(strSql);

            string strFields = "";
            string strValues = "";

            foreach (DataRow dr in dt.Rows)
            {
                string field = dr["field"].ToString();
                string type = dr["type"].ToString();
                string nullvalue = dr["null"].ToString();
                string key = dr["key"].ToString();
                string extra = dr["extra"].ToString();
                string comment = dr["comment"].ToString();

                if (extra.IndexOf("auto_increment") != -1)
                {
                    continue;
                }

                strFields += "`" + field + "`,";
                strValues += "'1',";
            }

            //foreach (DataColumn dc in dt.Columns)
            //{
            //    strFields += "`" + dc.ColumnName + "`,";
            //    strValues += "'1',";

            //}

            if (strValues.Trim() != "")
            {
                strFields = strFields.Substring(0, strFields.Length - 1);
                strValues = strValues.Substring(0, strValues.Length - 1);

                strTrans.Append("insert into `" + database + "`.`" + table + "`(" + strFields + ") values(" + strValues + ")\n");
            }

            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
    }

    /// <summary>
    /// delete
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void toolStripButton3_Click(object sender, EventArgs e)
    {
        //delete from grade where 数学＝0

        StringBuilder strTrans = new StringBuilder();

        this.richTextBox1.Text = "";

        if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
        {
            strTrans.Append("delete from grade where 数学＝0");
            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
        else
        {
            string database = this.treeView1.SelectedNode.Parent.Text;
            string table = this.treeView1.SelectedNode.Text;

            strTrans.Append("delete from `" + database + "`.`" + table + "`");
            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
    }

    /// <summary>
    /// update
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void toolStripButton4_Click(object sender, EventArgs e)
    {
        //update grade set 数学＝数学+10

        StringBuilder strTrans = new StringBuilder();

        this.richTextBox1.Text = "";
        if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
        {
            strTrans.Append("update grade set 数学＝数学+10 where id=1\n");
            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
        else
        {
            string database = this.treeView1.SelectedNode.Parent.Text;
            string table = this.treeView1.SelectedNode.Text;

            //string strSql = "select * FROM " + database + "." + table + "";
            //DataTable dt = MysqlHelper.GetDataTable(strSql);

            string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
            DataTable dt = MysqlHelper.GetDataTable(strSql);

            string strFields = "";
            string strValues = "";

            foreach (DataRow dr in dt.Rows)
            {
                string field = dr["field"].ToString();
                string type = dr["type"].ToString();
                string nullvalue = dr["null"].ToString();
                string key = dr["key"].ToString();
                string extra = dr["extra"].ToString();
                string comment = dr["comment"].ToString();

                if (extra.IndexOf("auto_increment") != -1)
                {
                    continue;
                }

                strFields += "`" + field + "`='1',";
            }

            strFields = strFields.Substring(0, strFields.Length - 1);
            //strValues = strValues.Substring(0, strValues.Length - 1);

            strTrans.Append("update  `" + database + "`.`" + table + "`set " + strFields + "");
            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
    }

    /// <summary>
    /// select
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void toolStripButton5_Click(object sender, EventArgs e)
    {
        //update grade set 数学＝数学+10

        StringBuilder strTrans = new StringBuilder();

        this.richTextBox1.Text = "";
        if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
        {
            strTrans.Append("select * from sys.sysobjects   where id=1\n");
            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
        else
        {
            string database = this.treeView1.SelectedNode.Parent.Text;
            string table = this.treeView1.SelectedNode.Text;

            //string strSql = "select * FROM " + database + "." + table + "";
            //DataTable dt = MysqlHelper.GetDataTable(strSql);

            string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
            DataTable dt = MysqlHelper.GetDataTable(strSql);

            string strFields = "";
            string strValues = "";

            foreach (DataRow dr in dt.Rows)
            {
                string field = dr["field"].ToString();
                string type = dr["type"].ToString();
                string nullvalue = dr["null"].ToString();
                string key = dr["key"].ToString();
                string extra = dr["extra"].ToString();
                string comment = dr["comment"].ToString();

                //if (extra.IndexOf("auto_increment") != -1)
                //{
                //    continue;
                //}

                strFields += "`" + field + "`,";
            }

            strFields = strFields.Substring(0, strFields.Length - 1);
            //strValues = strValues.Substring(0, strValues.Length - 1);

            strTrans.Append("select " + strFields + " from `" + database + "`.`" + table + "`");
            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
    }

    /// <summary>
    /// alter
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void toolStripButton6_Click(object sender, EventArgs e)
    {
        //alter table table1 add No_id varchar(50)  null
        StringBuilder strTrans = new StringBuilder();

        this.richTextBox1.Text = "";

        if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
        {
            strTrans.Append("/*\n");
            strTrans.Append("mysql> CREATE TABLE t1 (a INTEGER,b CHAR(10));\n");
            strTrans.Append("把表t1重新命名为t2：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t1 RENAME t2;\n");
            strTrans.Append("把列a从INTERGER更改为TINYINT NOT NULL（名称保持不变），并把列b从CHAR(10)更改为CHAR(20)，同时把列b重新命名为列c：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);\n");
            strTrans.Append("添加一个新的TIMESTAMP列，名称为d：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t2 ADD d TIMESTAMP;\n");
            strTrans.Append("在列d和列a中添加索引：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t2 ADD INDEX (d), ADD INDEX (a);\n");
            strTrans.Append("删除列c：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t2 DROP COLUMN c;\n");
            strTrans.Append("添加一个新的AUTO_INCREMENT整数列，名称为c：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,\n");
            strTrans.Append("    ->     ADD PRIMARY KEY (c);\n");
            strTrans.Append("*/\n");

            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
        else
        {
            string database = this.treeView1.SelectedNode.Parent.Text;
            string table = this.treeView1.SelectedNode.Text;

            strTrans.Append("alter  table `" + database + "`.`" + table + "` add No_id varchar(50)  null ");

            strTrans.Append("\n");

            strTrans.Append("/*\n");
            strTrans.Append("mysql> CREATE TABLE t1 (a INTEGER,b CHAR(10));\n");
            strTrans.Append("把表t1重新命名为t2：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t1 RENAME t2;\n");
            strTrans.Append("把列a从INTERGER更改为TINYINT NOT NULL（名称保持不变），并把列b从CHAR(10)更改为CHAR(20)，同时把列b重新命名为列c：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);\n");
            strTrans.Append("添加一个新的TIMESTAMP列，名称为d：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t2 ADD d TIMESTAMP;\n");
            strTrans.Append("在列d和列a中添加索引：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t2 ADD INDEX (d), ADD INDEX (a);\n");
            strTrans.Append("删除列c：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t2 DROP COLUMN c;\n");
            strTrans.Append("添加一个新的AUTO_INCREMENT整数列，名称为c：\n");
            strTrans.Append("\n");
            strTrans.Append("mysql> ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,\n");
            strTrans.Append("    ->     ADD PRIMARY KEY (c);\n");
            strTrans.Append("*/\n");

            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
    }

    /// <summary>
    /// drop
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void toolStripButton7_Click(object sender, EventArgs e)
    {
        //DROP TABLE  `name` ;

        StringBuilder strTrans = new StringBuilder();

        this.richTextBox1.Text = "";

        if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
        {
            strTrans.Append("DROP TABLE  `name` ; ");
            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
        else
        {
            string database = this.treeView1.SelectedNode.Parent.Text;
            string table = this.treeView1.SelectedNode.Text;

            strTrans.Append("DROP TABLE   `" + database + "`.`" + table + "`; ");

            strTrans.Append("\n");

            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
    }

    private void MySql综合_MouseClick(object sender, MouseEventArgs e)
    {
    }

    private void treeView1_Click(object sender, EventArgs e)
    {
    }

    private void treeView1_MouseClick(object sender, MouseEventArgs e)
    {
        try
        {
            if (e.Button.ToString() == "Right")
            {
                string strTemp = this.treeView1.SelectedNode.Text;

                string strTextBox = richTextBox1.Text;
                string strResult = "";

                int i = this.richTextBox1.SelectionStart;
                strResult = strTextBox.Substring(0, i);
                strResult += strTemp;
                strResult += strTextBox.Substring(i, strTextBox.Length - i);

                richTextBox1.Text = strResult;
                richTextBox1.SelectionStart = i;
            }
        }
        catch (Exception ee) { }
    }

    /// <summary>
    /// create
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void toolStripButton8_Click(object sender, EventArgs e)
    {
        StringBuilder strTrans = new StringBuilder();

        strTrans.Append("USE  `test`;\n");
        strTrans.Append("\n");
        strTrans.Append("/*Table structure for table`member` */\n");
        strTrans.Append("\n");
        strTrans.Append("DROP TABLE IF EXISTS`member`;\n");
        strTrans.Append("CREATE TABLE`member` (\n");
        strTrans.Append("`id` int(11) not null  PRIMARY KEY  AUTO_INCREMENT COMMENT '', \n");
        strTrans.Append("`name` varchar(50) null COMMENT '', \n");
        strTrans.Append("`pass` varchar(50) null COMMENT '', \n");
        strTrans.Append("`rank` int(11) null COMMENT '', \n");
        strTrans.Append("`status` int(1) null COMMENT '' \n");
        strTrans.Append(")  DEFAULT CHARSET=latin1;\n");
        strTrans.Append("\n");
        strTrans.Append("\n");
        strTrans.Append("/*\n");
        strTrans.Append("int(10)\n");
        strTrans.Append("text\n");
        strTrans.Append("varchar(30)\n");
        strTrans.Append("datetime\n");
        strTrans.Append("*/\n");

        this.richTextBox1.Text = strTrans.ToString();
        return;
    }

    private void toolStripButton9_Click(object sender, EventArgs e)
    {
        StringBuilder strTrans = new StringBuilder();
        this.richTextBox1.Text = "";

        if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
        {
            strTrans.Append("insert into table_detail(table_name,`field`,name) values(123,'周润发',70)\n");
            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
        else
        {
            string database = this.treeView1.SelectedNode.Parent.Text;
            string table = this.treeView1.SelectedNode.Text;

            strTrans.Append("use " + database + ";\n");

            string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
            DataTable dt = MysqlHelper.GetDataTable(strSql);

            string strFields = "";
            string strValues = "";

            foreach (DataRow dr in dt.Rows)
            {
                string field = dr["field"].ToString();
                string type = dr["type"].ToString();
                string nullvalue = dr["null"].ToString();
                string key = dr["key"].ToString();
                string extra = dr["extra"].ToString();
                string comment = dr["comment"].ToString();

                strValues += "('" + table + "','" + field + "','" + comment + "')" + ",";
            }

            //strFields = strFields.Substring(0, strFields.Length - 1);
            strValues = strValues.Substring(0, strValues.Length - 1);

            strTrans.Append("insert into table_detail(table_name,`field`,name) values" + strValues + "\n");
            //strTrans.Append("update  `" + database + "`.`" + table + "`set " + strFields + "");
            this.richTextBox1.Text = strTrans.ToString();
            return;
        }
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
    }
}